﻿using ArtMath.Component.Point;
using ArtMath.Core.Data;
using System;
using System.Collections.Generic;
using ArtMath.CommonType;

namespace ArtMath.Component.Manager
{
    [Serializable]
    public class CrossoverPointsManager_B : CrossoverPointsManagerBase
    {
        public CrossoverPointsManager_B(SketchPad pad, string name, LineBase line1, LineBase line2) : base(pad, name, line1, line2, false)
        {
        }
        public override void PointMoving_ManagedComponentsLimit(Base component)
        {
            Dictionary<CrossoverPointBase, PointD> dic = GetDistribution(out CrossoverPointBase[] rdpt, out PointD[] rdco);
            if (dic != null)
            {
                foreach (KeyValuePair<CrossoverPointBase, PointD> item in dic)
                {
                    item.Key.ObjectState = ObjectState.Normal;
                    item.Key.NewPxy = item.Value;
                }
                foreach (KeyValuePair<CrossoverPointBase, PointD> item in dic)
                {
                    item.Key.Move(item.Value, true);
                }
            }
            if (rdpt != null)
            {
                foreach (CrossoverPointBase item in rdpt)
                {
                    item.ObjectState = ObjectState.Deleted;
                }
            }
            //PointD[] tmp = CurveIntersection.GetCrossPoints(Line1, Line2);
            //if (tmp == null)
            //{

            //}
            //else
            //{
            //    for (int i = 0; i < flag.Length; i++)
            //    {
            //        double minDist = double.PositiveInfinity;
            //        int minIndex = -1;
            //        for (int j = 0; j < tmp.Length; j++)
            //        {
            //            if (!flag[i])
            //            {
            //                double dist = tmp[j].DistanceTo(Points[i].Pxy);
            //                if (dist < minDist)
            //                {
            //                    minDist = dist;
            //                    minIndex = j;
            //                }
            //            }
            //        }
            //        if (minIndex < 0)//说明所有交点的坐标都已刷新
            //        {
            //            break;
            //        }
            //        else
            //        {
            //            Points[minIndex].Deleted = false;
            //            if (Points[minIndex].Flag)
            //            {
            //                if (Points[minIndex].NewPxy.AlmostEquals(tmp[i]))
            //                {
            //                    Points[minIndex].WaitToLimit.Remove(component as LineBase);
            //                }
            //                else
            //                {
            //                    throw new PointMovingException(this, $"\"{Points[minIndex].Name}\"有多个不同的目标坐标：{Pad.PxyToMath(Points[minIndex].NewPxy)}和{Pad.PxyToMath(tmp[i])}");
            //                }
            //            }
            //            else
            //            {
            //                Points[minIndex].Move(tmp[i]);
            //            }
            //            flag[minIndex] = true;
            //        }
            //    }
            //}
        }
    }
}
